<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>留言板</title>
	<link rel="stylesheet" href="./tpl/layui/css/layui.css">
	<script src="./tpl/layui/layui.js"></script>
  <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
	<ul class="layui-nav" lay-filter="">
	  <li class="layui-nav-item"><a href="">最新活动</a></li>
	  <li class="layui-nav-item layui-this"><a href="">产品</a></li>
	  <li class="layui-nav-item"><a href="">大数据</a></li>
	  <li class="layui-nav-item">
	    <a href="javascript:;">解决方案</a>
	    <dl class="layui-nav-child"> <!-- 二级菜单 -->
	      <dd><a href="">移动模块</a></dd>
	      <dd><a href="">后台模版</a></dd>
	      <dd><a href="">电商平台</a></dd>
	    </dl>
	  </li>
    <?php if(isset($_SESSION['user_id'])){ ?>
        <li class="layui-nav-item"><?php echo $_SESSION['username']; ?></li>
        <li class="layui-nav-item"><a href="./out.php">退出</a></li>
    <?php }else{ ?>
      <li class="layui-nav-item"><a href="./login.php">登录</a></li>
      <li class="layui-nav-item"><a href="./reg.php">注册</a></li>

    <?php } ?>


	</ul>
	<div style="padding: 20px; background-color: #F2F2F2;">
      <div class="layui-row layui-col-space15">
        <?php foreach ($data as  $row) { ?>
        <div class="layui-col-md3">
          <div class="layui-card">
            <div class="layui-card-header"><?php echo $row['username']; ?>
            	<div style="float: right;">
            		<a href="javascript:;"  class="layui-btn layui-btn-sm layui-btn-danger del" msg-id="<?php echo $row['msg_id'] ?>">删除</a>
            		<a href="./edit.php?msg_id=<?php echo $row['msg_id'] ?>" class="layui-btn layui-btn-sm">修改</a>
            	</div>
            </div>
            <div class="layui-card-body">
              <?php echo $row['content']; ?>(<?php echo Ftime($row['create_time']); ?>)
            </div>
          </div>
        </div>
         <?php } ?>
      </div>
      <form class="layui-form" style="margin-top: 50px" action="">
		  <div class="layui-form-item layui-form-text">
		    <div class="layui-input-block">
		      <textarea name="content" placeholder="请输入内容" class="layui-textarea"></textarea>
		    </div>
		  </div>
		  <div class="layui-form-item">
		    <div class="layui-input-block">
		      <button class="layui-btn" lay-submit lay-filter="add">立即发布</button>
		    </div>
		  </div>
		</form>
    </div>
	<script>
		//注意：导航 依赖 element 模块，否则无法进行功能性操作
		layui.use(['element','form'], function(){
		  var element = layui.element;
		  var form = layui.form;

      $('.del').click(function(event) {
           msg_id = $(this).attr('msg-id')
          layer.confirm('你真的要删除吗？', {icon: 5},function(){

              $.post('./del.php',{msg_id,msg_id}, function(res, textStatus, xhr) {
                  if(res.error==1){
                    layer.msg(res.msg, function(){});
                  }else{
                    layer.alert(res.msg, {icon: 6},function () {
                        location.reload();
                    });
                  }
              },'json');
          });
      });		  
		  
      //监听提交
      form.on('submit(add)', function(data){
        // layer.msg(JSON.stringify(data.field));

        $.post('./addmsg.php',data.field, function(res, textStatus, xhr) {

            if(res.error==1){
              layer.msg(res.msg, function(){});
            }else{
              layer.alert(res.msg, {icon: 6},function () {
                  location.reload();
              });
            }
        },'json');

        return false;
      });

		});
	</script>
</body>
</html>